package com.flyee.io.local{
	
	import com.flyee.communicate.Communicate;
	import com.flyee.utils.Map;
	import com.flyee.utils.MyLog;
	
	import flash.events.StatusEvent;
	import flash.net.LocalConnection;	
	
	public class ConnectBean extends LocalConnection{
		
		private var connName : String
		
		private var communicateMap : Map = new Map();
		
		public function ConnectBean(name : String){
			super();
			connName = name;
			this.client = this;
			this.connect(connName + "/down");
        	this.addEventListener(StatusEvent.STATUS, onStatus);
		}
		
		public function receiveHandler(commName : String, ... args) : void {
			var communicate : Communicate = this.communicateMap.get(commName);
			if(communicate==null){
				MyLog.debug("found no communicate");
				return ;	
			}
			communicate.response(args);
		}
		
		public function registerCommunicate(name : String, comm : Communicate) : void {
			communicateMap.put(name, comm);
		}
		
		public function cancelCommunicate(name : String) : void {
			communicateMap.remove(name);
		}
		
		public function sendMessage(commName : String, ... args) : void {
			this.send(connName+"/up", "receiveHandler", commName, args);
		}
		
		private function onStatus(event:StatusEvent):void {
            switch (event.level) {
                case "status":
                    MyLog.debug("LocalConnection.send() succeeded");
                    break;
                case "error":
                    MyLog.debug("LocalConnection.send() failed");
                    break;
            }
        }

	}
}